import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/login"
  },
  {
    path: '/home',
    component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue'),
    children: [
      {
        path: '/home',
        redirect: "/home/homes"
      },
      {
        path: '/home/homes',
        title: "房源管理",
        use: ['管理员', '员工'],
        component: () => import(/* webpackChunkName: "about" */ '../views/Homes'),
        meta:{
          isload:true
        }
      },
      {
        path: '/home/vadio',
        title: "直播管理",
        use: ['管理员'],
        component: () => import(/* webpackChunkName: "about" */ '../views/Vadio'),
        meta:{
          isload:true
        }
      },
      {
        path: '/home/teambuy',
        title: "团购管理",
        use: ['管理员'],
        component: () => import(/* webpackChunkName: "about" */ '../views/TeamBuy'),
        meta:{
          isload:true
        }
      },
      {
        path: '/home/audit',
        title: "审核管理",
        use: ['管理员'],
        component: () => import(/* webpackChunkName: "about" */ '../views/Audit'),
        meta:{
          isload:true
        }
      },
      {
        path: '/home/order',
        title: "订单管理",
        use: ['管理员'],
        component: () => import(/* webpackChunkName: "about" */ '../views/Order'),
        meta:{
          isload:true
        }
      }, {
        path: '/home/information',
        title: "资讯管理",
        use: ['管理员'],
        component: () => import(/* webpackChunkName: "about" */ '../views/Information'),
        meta:{
          isload:true
        }
      },
      {
        path: '/home/broker',
        title: "经纪人管理",
        use: ['管理员'],
        component: () => import(/* webpackChunkName: "about" */ '../views/Broker'),
        meta:{
          isload:true
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '/register',
    component: () => import('../views/Register')
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.meta.isload){
    if(localStorage.getItem('token')){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})

export default router